package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.entities.login.Label;
import com.maiji.cloud.entities.shopingmall.LabelMiddleGoods;
import com.maiji.cloud.mapper.LabelMiddleGoodsMapper;
import com.maiji.cloud.microservice.LabelService;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.service.LabelMiddleGoodsService;
import com.maiji.cloud.utils.UUID_MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class LabelMiddleGoodsServiceImpl extends ServiceImpl<LabelMiddleGoodsMapper, LabelMiddleGoods> implements LabelMiddleGoodsService {

    @Autowired
    private LabelService labelService;

    @Override
    public BaseResDto editLabelMidGoods(String goodsId, String labelName, Boolean labelAdd) {
        Label label = labelService.selectOne("商品", labelName);
        if (labelAdd){
            LabelMiddleGoods labelMiddleGoods = new LabelMiddleGoods()
                    .setUuId(UUID_MD5.getUUID()).setGoodsId(goodsId).setLabelId(label.getUuId());
            if (! insert(labelMiddleGoods)) return new BaseResDto(Status.ERROR);
        } else {
            EntityWrapper<LabelMiddleGoods> entityWrapper = new EntityWrapper<>();
            entityWrapper.eq("goods_id", goodsId);
            entityWrapper.eq("label_id", label.getUuId());
            if (! delete(entityWrapper)) return new BaseResDto(Status.ERROR);
        }
        return new BaseResDto(Status.SUCCESS);
    }
}
